#include "StdAfx.h"
#include "HandelControl.h"
void HandelControl::SetCurrentPosition(float winx,float winy)
{
	float x=((winx/((this->width-2)/2))-2);
	float y=-((winy/((this->height-2)/2))-2);
	float z=sqrt(4.0f-x*x+y*y);
	this->lastPosition[0]=this->currentPosition[0];
	this->lastPosition[1]=this->currentPosition[1];
	this->lastPosition[2]=this->currentPosition[2];
	this->currentPosition[0]=x;
	this->currentPosition[1]=y;
	this->currentPosition[2]=z;
}

void HandelControl::GetChangeMatrix(float &angel,float &x,float &y,float &z)
{
	float Ux=this->currentPosition[0];
	float Uy=this->currentPosition[1];
	float Uz=this->currentPosition[2];

	float Vx=this->lastPosition[0];
	float Vy=this->lastPosition[1];
	float Vz=this->lastPosition[2];

	x=Uy*Vz-Uz*Vy;
	y=Uz*Vx-Ux*Vz;
	z=Ux*Vy-Uy*Vx;
	GLfloat s=Ux*Vx+Uy*Vy+Uz*Vz;
	GLfloat len=sqrt(Ux*Ux+Uy*Uy+Uz*Uz)*sqrt(Vx*Vx+Vy*Vy+Vz*Vz);
	angel=-acos(s/len)*180;
}

void HandelControl::SetViewSize(float width,float height)
{
	this->width=width;
	this->height=height;
}
void HandelControl::SetLBtnClickDown(float winx,float winy)
{
	this->leftBtnDownPos[0]=winx;
	this->leftBtnDownPos[1]=winy;
	this->leftButtonDown=true;
	this->SetCurrentPosition(winx,winy);
}
void HandelControl::SetLBtnClickUp(float winx,float winy)
{
	this->leftButtonDown=false;
}
HandelControl::HandelControl()
{
	memset(this->currentPosition,0.0f,3*sizeof(float));
	memset(this->lastPosition,0.0f,3*sizeof(float));
	this->width=0.0f;
	this->height=0.0f;
	this->leftButtonDown=false;
	this->radius=1.0f;
	this->lastX=0.0f;
	this->lastY=0.0f;
}

HandelControl::~HandelControl()
{

}
